package me.zhengjie.minbearwcs.dao.storage.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.minbearwcs.dao.storage.inf.SmallStorageDao;
import me.zhengjie.minbearwcs.entity.Mapper.storage.SmallStorageMapper;
import me.zhengjie.minbearwcs.entity.PO.wcs.storage.SmallStorage;
import org.apache.ibatis.javassist.tools.rmi.Sample;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class SmallStorageDaoImpl implements SmallStorageDao {

    private final SmallStorageMapper mapper;

    @Override
    public boolean updateInboundFreezePallet(Integer id, String freezePallet) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(SmallStorage::getId, id)
                .set(SmallStorage::getInboundFreezePallet, freezePallet)
                .update();
    }

    @Override
    public String selectSmallStorageByQrPoint(String qrPoint) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getQrPoint, qrPoint)
                .select(SmallStorage::getSmallStorageCode)
                .one()
                .getSmallStorageCode();
    }

    @Override
    public boolean updatePalletCodeByQrPoint(String qrPoint, String PalletCode) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(SmallStorage::getQrPoint, qrPoint)
                .set(SmallStorage::getPalletCode, PalletCode)
                .update();
    }

    @Override
    public String selectPalletCodeByQrpoint(String qrPoint) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getQrPoint, qrPoint)
                .one()
                .getPalletCode();
    }

    @Override
    public boolean judgeStorageCanUseByQrPoint(String qrPoint) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getQrPoint, qrPoint)
                .isNull(SmallStorage::getPalletCode)
                .isNull(SmallStorage::getInboundFreezePallet)
                .isNull(SmallStorage::getOutboundFreezePallet)
                .count() > 0;
    }

    @Override
    public boolean clearStoragePallet(String smallStorageCode) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(SmallStorage::getSmallStorageCode, smallStorageCode)
                .set(SmallStorage::getPalletCode, null)
                .set(SmallStorage::getInboundFreezePallet, null)
                .set(SmallStorage::getOutboundFreezePallet, null)
                .update();
    }

    @Override
    public boolean updatePalletCodeAndClearInboundFreeze(String smallStorageCode, String palletCode) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(SmallStorage::getSmallStorageCode, smallStorageCode)
                .set(SmallStorage::getInboundFreezePallet, null)
                .set(SmallStorage::getPalletCode, palletCode)
                .update();
    }

    @Override
    public List<SmallStorage> selectSmallStorageByMediunStorage(String medium) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getMediumStorageCode, medium)
                .orderByAsc(SmallStorage::getColumnNum)
                .list();
    }

    @Override
    public String selectMediumStorageByQrPoint(String qrPoint) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getQrPoint, qrPoint)
                .select(SmallStorage::getMediumStorageCode)
                .one()
                .getMediumStorageCode();
    }

    @Override
    public SmallStorage selectOneCanUseSmallStorageByMediunStorage(String medium) {
        Optional<SmallStorage> smallStorage = new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getMediumStorageCode, medium)
                .orderByAsc(SmallStorage::getColumnNum)
                .last("LIMIT 1")
                .oneOpt();
        return smallStorage.orElse(null);
    }

    @Override
    public SmallStorage selectBySmallStorage(String smallStorage) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getSmallStorageCode, smallStorage)
                .oneOpt()
                .orElse(null);
    }

    @Override
    public boolean updateOutboundFreezeBySmallStorageCode(String smallStorage, String outboundFreezePallet) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(SmallStorage::getSmallStorageCode, smallStorage)
                .set(SmallStorage::getOutboundFreezePallet, outboundFreezePallet)
                .update();
    }

    @Override
    public boolean updateInboundFreezePallet(String smallStorage, String freezePallet) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(SmallStorage::getSmallStorageCode, smallStorage)
                .set(SmallStorage::getInboundFreezePallet, freezePallet)
                .update();
    }

    @Override
    public boolean onlyCLearPalletCode(String smallStorage) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(SmallStorage::getSmallStorageCode, smallStorage)
                .set(SmallStorage::getPalletCode, null)
                .update();
    }

    @Override
    public SmallStorage selectByQrCode(String qrCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getQrPoint, qrCode)
                .one();
    }

    @Override
    public boolean insert(SmallStorage smallStorage) {
        return mapper.insert(smallStorage) == 1;
    }

    @Override
    public boolean clear() {
        return mapper.delete(new QueryWrapper<>()) == 1;
    }

    @Override
    public SmallStorage selectByPallet(String palletCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getPalletCode,palletCode)
                .oneOpt()
                .orElse(null);
    }

    @Override
    public SmallStorage selectByInboundFreezePallet(String palletCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getInboundFreezePallet,palletCode)
                .oneOpt()
                .orElse(null);
    }

    @Override
    public SmallStorage selectByOutboundFreezePallet(String palletCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getOutboundFreezePallet,palletCode)
                .oneOpt()
                .orElse(null);
    }

    @Override
    public SmallStorage selectPalletIsInStock(String palletCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(SmallStorage::getPalletCode,palletCode)
                .or()
                .eq(SmallStorage::getOutboundFreezePallet,palletCode)
                .or()
                .eq(SmallStorage::getOutboundFreezePallet,palletCode)
                .oneOpt()
                .orElse(null);
    }

    @Override
    public List<SmallStorage> selectList() {
        return new LambdaQueryChainWrapper<>(mapper).list();
    }
}
